package com.danlu.admin.utils;

import java.io.OutputStream;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class ExcelCommon {

	private static WritableWorkbook wts;

	public ExcelCommon(OutputStream os) throws Exception {
		wts = Workbook.createWorkbook(os);
	}
	
	public static WritableWorkbook getWritableWorkbook(OutputStream os)throws Exception
	{
		wts = Workbook.createWorkbook(os);
		return wts;
	}

	/**
	 * create excel title
	 * 
	 * @param name
	 * @param os
	 * @param titleList
	 * @return WritableSheet
	 * @throws Exception
	 */
	public static WritableSheet excelOut(String name, List titleList)
			throws Exception {

		WritableSheet wsheet = wts.createSheet(name, 0);

		for (int i = 0; i < titleList.size(); i++) {
			String title = (String) titleList.get(i);
			wsheet.addCell(new Label(i, 0, title));
		}

		return wsheet;
	}

	public static WritableSheet excelOut(String name, int sheetNum,
			List titleList) throws Exception {

		WritableSheet wsheet = wts.createSheet(name, sheetNum);

		for (int i = 0; i < titleList.size(); i++) {
			String title = (String) titleList.get(i);
			wsheet.addCell(new Label(i, 0, title));
		}

		return wsheet;
	}
	
	/**
	 * 在第rowCount+1行创建单元格
	 * @param name
	 * @param sheetNum
	 * @param rowCount   行号
	 * @param titleList
	 * @return
	 * @throws Exception
	 */
	public static WritableSheet excelOut(String name, int sheetNum,int rowCount,
			List titleList) throws Exception {
		WritableSheet wsheet = wts.createSheet(name, sheetNum);
		for (int i = 0; i < titleList.size(); i++) {
			String title = (String) titleList.get(i);
			wsheet.addCell(new Label(i, rowCount, title));
		}
		return wsheet;
	}

	public static WritableWorkbook getWritableWorkbook() {
		return wts;
	}

}
